const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const DataStore = require('./renderer/MusicDataStore')

const myStore = new DataStore({'name':'Music Data'})

class AppWindow extends BrowserWindow {
	constructor(config, fileLocation) {
		const basicConfig = {
			width: 800,
			height: 600,
			webPreferences: {
				nodeIntegration: true
			}
		}
		// const finalConfig = Object.assign(basicConfig, config)
		const finalConfig = { ...basicConfig, ...config }
		super(finalConfig)

		this.loadFile(fileLocation)
		this.once('ready-to-show', () => {
			this.show()
		})
	}
}

app.on('ready', () => {
	const mainWindow = new AppWindow({}, './renderer/index.html')
	mainWindow.loadFile('./renderer/index.html')

	// 打开添加音乐窗口
	ipcMain.on('add-music-window', () => {
		const addWindow = new AppWindow({
			width: 500,
			height: 400,
			parent: mainWindow
		}, './renderer/add.html')
	})

	// 打开系统弹窗
	ipcMain.on('open-music-file', (event) => {
		dialog.showOpenDialog({
			properties: ['openFile', 'multiSelections'],
			filters: [{ name: 'Music', extensions: ['mp3'] }]
		}, (files) => {
			if (files) {
				event.sender.send('selected-file', files)
			}
		})
	})
	
	// 保存
	ipcMain.on('save-tracks', (event, tracks)=>{
		const updatedTracks = myStore.addTracks(tracks).getTracks();
		console.log(updatedTracks);
	})
})
